﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using SCRUM.Core.Configuration;

namespace SCRUM.Core.BusinessObjects
{
    /// <summary>
    /// Business object which holds following information:
    ///  * ID - object's unique identifier (among objects of same type)
    ///  * Name - name of object
    ///  * Estimation - estimation of total work on object
    ///  * Progress - progress of total work on object (in%)
    ///  * LastChange - date of last changes made to object
    /// </summary>
    [Serializable()]
    public abstract class HierarchicalBusinessObject:  NamedBusinessObject
    {
        /// <summary>
        /// Gets or sets the estimation of total work on object.
        /// </summary>
        /// <value>The estimation.</value>
        public double Estimation { get; set; }

        /// <summary>
        /// Gets or sets the progress of total work on object (in%)
        /// </summary>
        /// <value>The progress.</value>
        public double Progress { get; set; }

        /// <summary>
        /// Gets or sets the date of last changes made to object.
        /// </summary>
        /// <value>The last change.</value>
        public DateTime LastChange { get; set; }

        /// <summary>
        /// Gets or sets the superior item's ID.
        /// </summary>
        /// <value>The superior item ID.</value>
        public abstract int SuperiorItemID { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="HierarchicalBusinessObject"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="estimation">The estimation.</param>
        /// <param name="progress">The progress.</param>
        /// <param name="change">The change.</param>
        protected HierarchicalBusinessObject(int id, string name, double estimation, double progress, DateTime change)
            :base(id,name)
        {
            this.Estimation = estimation;
            this.Progress = ComputeProgress(progress, estimation);
            this.LastChange = change;
        }

        /// <summary>
        /// Computes the progress in % from current and estimation.
        /// </summary>
        /// <param name="current">The current.</param>
        /// <param name="estimation">The estimation.</param>
        /// <returns></returns>
        protected static int ComputeProgress(double current, double estimation)
        {
            double ret = estimation != 0 ? current / estimation : 0;
            return ret > 0 && ret < 0.01 ? 1 : System.Convert.ToInt32(ret*100);
        }

    }


}
